package com.sumainfor.wx.tenant.controller;

import com.sumainfor.common.util.FieldCheckUtil;
import com.sumainfor.common.util.JsonResult;
import com.sumainfor.common.utlis.PageUtils;
import com.sumainfor.common.utlis.Pager;
import com.sumainfor.wx.tenant.service.TenantCouponsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *
 * <p>
 *
 * @author: Mr.Wang
 * @create: 2019-03-26 18:24
 **/
@RestController
@RequestMapping("/tenantCoupons")
public class TenantCouponsController {

    Logger log= LoggerFactory.getLogger(TenantCouponsController.class);

    @Autowired
    private TenantCouponsService tenantCouponsService;


    /**
     * <p>
     * 根据租客id获取优惠券详情
     * </p>
     *
     * @author: zhoucong
     * @since: 2019-03-26
     */
    @GetMapping("/getTenantCouponsByTenId")
    public JsonResult getCouponByTenId(@RequestParam Map<String,Object> params, Pager pager){
        log.info("params------------------"+params);
        log.info("pager------------------"+pager);
        JsonResult result = new JsonResult();
        pager.setPagerNecessary(params, pager);
        PageUtils pageUtils = new PageUtils();
        System.out.println("params"+params);
        if(!FieldCheckUtil.isMapFieldNotNull(params ,new String[]{"TenId"})){
            return  result.putFailed("服务器繁忙...");
        }
        Integer cout = tenantCouponsService.getTenantCouponsByTenIdCount(params);
        System.out.println("cout"+cout);
        if (cout == 0) {
            return pageUtils.getJsonResult(new ArrayList<Map<String, Object>>(), params, cout);
        }else {
            List<Map<String, Object>> getTenantCouponsByTenId = tenantCouponsService.getTenantCouponsByTenId(params);
            System.out.println("getTenantCoupons"+getTenantCouponsByTenId);
            return pageUtils.getJsonResult(getTenantCouponsByTenId, params, cout);
        }

    }

    /**
    * <p>
    * 根据优惠券编码领取优惠券
    * </p>
    *
    * @author: zhoucong1
    * @since: 2019-03-26
    */
    @PostMapping("/getTenantCouponsByCouId")
    public JsonResult getCouponByCouId(@RequestBody Map<String,Object> params){
        JsonResult result = new JsonResult();
        if(!FieldCheckUtil.isMapFieldNotNull(params ,new String[]{"CouId"})){
            return  result.putFailed("优惠券编码不能为空");
        }
        return tenantCouponsService.getCouponByCouId(params);
    }

}
